package org.ssp.test;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.MailSSLSocketFactory;

/**
 * 
 * 内嵌图片和带附件的邮件
 * 
 * @author susp
 *
 */
public class EmailTest4 {
	public static void main(String[] args) {
		Properties prop=new Properties();
		prop.setProperty("mail.host", "smtp.qq.com");
		prop.setProperty("mail.transport.protocol", "smtp");
		prop.setProperty("mail.smtp.auth", "true");
		MailSSLSocketFactory sf=null;
		//ssl加密
		try {
			sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);

		} catch (GeneralSecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.ssl.socketFactory", sf);
		//创建session
		Session session=Session.getInstance(prop);
		session.setDebug(true);
		//创建transport
		Transport ts=null;
		try {
			ts=session.getTransport();
			//创建message
			Message message=createMiexedMail(session);
			//连接服务器，验证
			ts.connect("smtp.qq.com","1255292979","slhphqvgsujkjbgi");
			//发送邮件
			ts.sendMessage(message, message.getAllRecipients());
			
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				ts.close();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//创建正文带图片和带附件的邮件
	public static MimeMessage createMiexedMail(Session session){
		MimeMessage message=new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress("1255292979@qq.com"));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress("1255292979@qq.com"));
			message.setSubject("带内嵌图片和附件的邮件");
			
			//正文
			MimeBodyPart text=new MimeBodyPart();
			text.setContent("xxx这是女的xxx<br/><img src='cid:aaa.jpg'>","text/html;charset=utf-8");
			//图片
			MimeBodyPart image=new MimeBodyPart();
			image.setDataHandler(new DataHandler(new FileDataSource("src\\login-bg.jpg")));
			image.setContentID("aaa.jpg");
			//附件1
			MimeBodyPart attach1=new MimeBodyPart();
			DataHandler dh=new DataHandler(new FileDataSource("src\\login-bg.jpg"));
			attach1.setDataHandler(dh);
			attach1.setFileName(dh.getName());
			//附件2
			MimeBodyPart attach2=new MimeBodyPart();
			DataHandler dh2=new DataHandler(new FileDataSource("src\\db.properties"));
			attach2.setDataHandler(dh2);
			attach2.setFileName(dh2.getName());
			
			MimeMultipart mm1=new MimeMultipart();
			mm1.addBodyPart(text);
			mm1.addBodyPart(image);
			mm1.setSubType("related");
			
			MimeBodyPart content=new MimeBodyPart();
			content.setContent(mm1);
			
			MimeMultipart mm2=new MimeMultipart();
			mm2.addBodyPart(attach1);
			mm2.addBodyPart(attach2);
			mm2.addBodyPart(content);
			mm2.setSubType("mixed");
			message.setContent(mm2);
			message.saveChanges();
	
			
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
		
	}

	
}
